<?php

// ##############################################################################||
// #                                                                 
// #   MySmartBB Version 1.7.0	                                      
// #   http://www.MySmartBB.com                                      
// #   Copyright (c) 2008 by MySmartBB team                           
// #   license http://opensource.org/licenses/gpl-license.php GNU Public License
// #                                                             
// #   filename : forum.php                                            
// #   show subject list and sub-sections informations                                          
// #                                                                  
// ##############################################################################||

// ** General definitions **

     define('NOT_IN_INDEX',1);
     include('common.php');
     include('includes/pager.php');

     if ($_GET['show'] == 1)
     {
	 
         $id = intval($_GET['id']);

         $section_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $id . "'");
         $section_num   = $DB->sql_num_rows($section_query);
         $section_row   = $DB->sql_fetch_array($section_query);

	     $SF->html_title_page($section_row['title'] . ' - (Powered By MySmartBB Universal)');
			 
         if ($member_permission != 0)
         {
             $group_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "sectiongroup WHERE group_id='" . $member_row['usergroup'] . "' AND section_id='" . $section_row['id'] . "'");
             $group_row   = $DB->sql_fetch_array($group_query);
         }

         if ($member_permission == 0)
         {
             $group_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "sectiongroup WHERE group_id='7' AND section_id='" . $section_row['id'] . "'");
             $group_row   = $DB->sql_fetch_array($group_query);
         }

         if ($section_num <= 0)
         {
             $SF->error('القسم المطلوب غير موجود');
         }

         if ($group_row['view_section'] == 0)
         {
             $SF->error('المعذرة، لا يمكنك الاطلاع على هذا القسم');
         }

         if ($section_row['main_section'] == 1)
         {
             $SF->error('المعذرة، المنتدى المطلوب منتدى رئيسي');
         }

         if ($section_row['linksection'] == 1)
         {
             $update_visit = $DB->sql_query("UPDATE " . $db_prefix . "section SET linkvisitor=linkvisitor+1 WHERE id='$id'");
             $SF->msg('يرجى الانتظار .. سوف يتم تحويلك إلى ' . $section_row['linksite']);
             $SF->msg('<br /><a href='.$section_row['linksite'].'>ملاحظة: إذا كان متصحك لا يدعم الانتقال التلقائي اضغط هنا</a>');
             $SF->go_to($section_row['linksite'],3);
             $SF->MySmartBB_Exit();
         }

         $subject_num = $DB->sql_num_rows($DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE section='$id' AND delete_topic<>'1'"));

         $Smarty->assign('subject_num',$subject_num);

         if ($subject_num <= 0 and empty($section_row['section_password']))
         {
             $Smarty->assign('no_subject','<div align="center">لا توجد مواضيع في هذا القسم</div>');
         }

         if (!isset($_GET['page']) OR $_GET['page'] < 1)
         {
              $_GET['page'] = 1;
         }

         $page  = intval($_GET['page']);
         $start = ($info_row['perpage'] * ($page-1));

         $RP = new Pager($page);

         if (($section_row['hide_subject'] != 1) or ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] == 1))
         {
             $RP->SetPagerN($info_row['perpage'],$DB->sql_num_rows($DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE section='$id' AND delete_topic<>'1' AND stick<>'1'")));
         }

         if ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] != 1)
         {
             $RP->SetPagerN($info_row['perpage'],$DB->sql_num_rows($DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE section='$id' AND delete_topic<>'1' AND stick<>'1' AND writer='" . $SF->SafeSQL($member_row['username']) . "'")));
         }

         $Smarty->assign('print_page',$RP->PageNum('show=1&id=' . $id . $url_password,'#posts_table'));

         $Smarty->assign_by_ref('section_row',$section_row);

         if (!empty($section_row['section_password']) and $groupper_row['admincp_allow'] != 1 and empty($_GET['password']))
         {
             $Smarty->display('section_password.tpl');
             $SF->MySmartBB_Exit();
         }

         if (!empty($section_row['section_password']) and $groupper_row['admincp_allow'] != 1 and !empty($_GET['password']))
         {
             $check_password = $DB->sql_num_rows($DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='$id' AND section_password='" . base64_decode($_GET['password']) . "'"));
             if ($check_password <= 0)
             {
                 $SF->error('المعذرة، كلمة المرور غير صحيحة');
             }
             else
             {
                 $url_password = '&password=' . htmlspecialchars($_GET['password']);
                 $Smarty->assign('url_password',$url_password);
             }
         }

         if ($section_row['sub_section'] == 1)
         {
             $getmainsections_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE id='" . $section_row['from_sub_section'] . "'");
             $getmainsection_row         = $DB->sql_fetch_array($getmainsections_query);

             $Smarty->assign_by_ref('getmainsection_row',$getmainsection_row);
             $Smarty->assign('path_bar',"<a href=forum.php?show=1&id=".$getmainsection_row['id'].">".$getmainsection_row['title']."</a> <img src=image/ric2.png> ".$section_row['title']);
         }
         else
         {
             $Smarty->assign('path_bar',$section_row['title']);
         }

         $Smarty->display('path-bar.tpl');


         $announcement_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "announcement ORDER BY id DESC LIMIT 0,5");
         $announcement_num   = $DB->sql_num_rows($announcement_query);

         if ($announcement_num != 0)
         {
             $announcement_row = array();
             while ($row = $DB->sql_fetch_array($announcement_query))
             {
                 $announcement_row[] = $row;
                 $Smarty->assign('announcement_row',$announcement_row);
             }
         	$Smarty->assign('announcement_num',$announcement_num);
       		$Smarty->display('section_announcement.tpl');

         }


         $getsubsection_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE sub_section='1' AND from_sub_section='$id'");
         $getsubsection_num          = $DB->sql_fetch_array($getsubsection_query);

         if ($getsubsection_num > 0)
         {
             $subforums     =  array();
             $SectionGroup  =  array();

             eval($info_row['subforums_cache']);
             eval($info_row['sectiongroup_cache']);

             $forums_num     =  count($subforums);
             $usergroup_num  =  count($SectionGroup);

             if ($forums_num > 0)
             {
                 $Smarty->display('section_subsections.tpl');

                 for ($i = 0 ; $i <= $forums_num-1 ; $i++)
                 {
                     for ($x = 0 ; $x <= $usergroup_num ; $x++)
                     {
                         if ($SectionGroup[$x]['section_id'] == $subforums[$i]['id'])
                         {
                             if ($subforums[$i]['from_sub_section'] == $section_row['id'])
                             {
                                 if ($SectionGroup[$x]['group_id'] == $visitor_row['usergroup'])
                                 {
                                     if ($SectionGroup[$x]['view_section'] == 1)
                                     {

                                         $section_rowsection_admin = explode(",",$subforums[$i][section_admin]);
                                         unset($section_rowsection_admin[count($section_rowsection_admin)-1]);
                                         $Smarty->assign_by_ref('section_rowsection_admin',$section_rowsection_admin);

					 $last_cut=$SF->titlecut($subforums[$i]['last_subject'],20);
					 $Smarty->assign_by_ref('last_cut',$last_cut);

                                         $Smarty->assign_by_ref('section_row',$subforums[$i]);

                                         if ($member_permission == 1)
                                         {
                                             $lastreplyicon_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE wr_date>='" . $_COOKIE['MySBB_lastvisit'] . "' AND section='" . $forums[$i]['id'] . "' AND delete_topic<>'1'");
                                             $last_replyicon_num  = $DB->sql_num_rows($lastreplyicon_query);
                                         }
                                         $Smarty->assign_by_ref('last_replyicon_num',$last_replyicon_num);

                                         if ($subforums[$i]['linksection'] != 1)
                                         {
                                             $Smarty->display('homepage_sections2.tpl');
                                         }
                                         else
                                         {
                                             $Smarty->display('homepage_sections-link.tpl');
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }

         if ($info_row['online_now_section'] == 1)
         {
             if ($groupper_row['show_hidden'] != 1)
             {
                 if ($info_row['show_onlineguest'] != 1)
                 {
                     $sectionvisitor_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE username<>'زائر' AND user_id<>'' AND hide_browse<>'1' AND notinindex_id='" . intval($_GET['id']) . "' ORDER BY user_id ASC");
                 }

                 if ($info_row['show_onlineguest'] == 1)
                 {
                     $sectionvisitor_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE hide_browse<>'1' AND notinindex_id='" . intval($_GET['id']) . "' ORDER BY user_id ASC");
                 }
             }

             if ($groupper_row['show_hidden'] == 1)
             {
                 if ($info_row['show_onlineguest'] != 1)
                 {
                     $sectionvisitor_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE username<>'زائر' AND user_id<>'' AND notinindex_id='" . intval($_GET['id']) . "' ORDER BY user_id ASC");
                 }

                 if ($info_row['show_onlineguest'] == 1)
                 {
                     $sectionvisitor_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE notinindex_id='" . intval($_GET['id']) . "' ORDER BY user_id ASC");
                 }
             }

             $sectionvisitors_row = array();
             while ($sectionvisitor_row = $DB->sql_fetch_array($sectionvisitor_query))
             {
                 $sectionvisitors_row[] = $sectionvisitor_row;
                 $Smarty->assign('sectionvisitors_row',$sectionvisitors_row);
             }
         }

         $admin_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "sectionadmin WHERE section_id='$id'");
         $admin_num         = $DB->sql_num_rows($admin_query);

         $Smarty->assign('admin_num',$admin_num);

         $admin_rows = array();
         while ($admin_row = $DB->sql_fetch_array($admin_query))
         {
             $admin_rows[] = $admin_row;
             $Smarty->assign('admin_rows',$admin_rows);
         }

         $Smarty->assign('admin_num',$admin_num);
         $Smarty->assign('id',$id);

         $Smarty->display('section_top.tpl');

         $sticksubject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE stick='1' AND section='$id' AND delete_topic<>'1' ORDER BY write_time DESC");
         $sticksubject_num   = $DB->sql_num_rows($sticksubject_query);

         $SF->subject_part($sticksubject_num);

         $LastReply = array();
         while ($sticksubjects_row = $DB->sql_fetch_array($sticksubject_query))
         {
             $SF->html('<tr><td class="row3 tdb" width="3%">');

             $sharereply_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "reply WHERE subject_id='" . $sticksubjects_row['id'] . "' AND writer='" . $_COOKIE['MySBB_username'] . "' LIMIT 0,1");
             $sharereply_num = $DB->sql_num_rows($sharereply_query);

             $lastsharereply_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "reply WHERE subject_id='" . $sticksubjects_row['id'] . "' AND writer='" . $_COOKIE['MySBB_username'] . "' AND delete_topic<>'1' ORDER BY id DESC LIMIT 0,1");
             $lastsharereply_num = $DB->sql_num_rows($sharereply_query);

             $reply_num = $sticksubjects_row['reply_number'];

             $lastreply_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "reply WHERE subject_id='" . $sticksubjects_row['id'] . "' AND delete_topic<>'1' ORDER BY id DESC LIMIT 0,1");
             $lastreply_num   = $DB->sql_num_rows($lastreply_query);

             eval($subjects_row['lastreply_cache']);

             $lastsharereply_num = in_array($member_row['username'],$LastReply);

             $reply_num = $sticksubjects_row['reply_number'];

             $SF->subject_part4($sticksubjects_row,$info_row,$member_row,$sharereply_num,$lastsharereply_num,$reply_num);
             $SF->subject_part4_2($sticksubjects_row,$info_row,$member_row,$url_password);
             $SF->subject_part5($sticksubjects_row,$lastreply_row,$lastreply_num);
         }

         $SF->subject_part3();

         $subject_d_order = 'ORDER BY write_time DESC';

         if ($section_row['subject_order'] == 1)
         {
             $subject_d_order = 'ORDER BY write_time DESC';
         }

         if ($section_row['subject_order'] == 2)
         {
             $subject_d_order = 'ORDER BY id DESC';
         }

         if ($section_row['subject_order'] == 3)
         {
             $subject_d_order = 'ORDER BY id ASC';
         }

         if ($_GET['order'] > 3 or empty($_GET['order']))
         {
             if (($section_row['hide_subject'] != 1) or ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] == 1))
             {
                 $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' $subject_d_order LIMIT $start," . $info_row['perpage'] ."");
             }

             if ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] != 1)
             {
                 $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND writer='" . $SF->SafeSQL($member_row['username']) . "' AND section='$id' $subject_d_order LIMIT $start," . $info_row['perpage'] ."");
             }

             $Smarty->assign('dont_print_SC',1);
         }

         if ($_GET['order'] == 1)
         {
             if ($_GET['order_type'] != 'DESC' and $_GET['order_type'] != 'ASC')
             {
                 $SF->error('الرابط الذي اتبعته غير صحيح');
             }

             if ($_GET['order_type'] == 'DESC')
             {
                 if (($section_row['hide_subject'] != 1) or ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] == 1))
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' ORDER BY write_time DESC LIMIT $start," . $info_row['perpage'] ."");
                 }

                 if ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] != 1)
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' AND writer='" . $SF->SafeSQL($member_row['username']) . "' ORDER BY write_time DESC LIMIT $start," . $info_row['perpage'] ."");
                 }
             }
             elseif ($_GET['order_type'] == 'ASC')
             {
                 if (($section_row['hide_subject'] != 1) or ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] == 1))
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' ORDER BY write_time ASC LIMIT $start," . $info_row['perpage'] ."");
                 }

                 if ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] != 1)
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' AND writer='" . $SF->SafeSQL($member_row['username']) . "' ORDER BY write_time ASC LIMIT $start," . $info_row['perpage'] ."");
                 }
             }
         }
         elseif ($_GET['order'] == 2)
         {
             if ($_GET['order_type'] != 'DESC' and $_GET['order_type'] != 'ASC')
             {
                 $SF->error('الرابط الذي اتبعته غير صحيح');
             }

             if ($_GET['order_type'] == 'DESC')
             {
                 if (($section_row['hide_subject'] != 1) or ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] == 1))
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' ORDER BY visitor DESC LIMIT $start," . $info_row['perpage'] ."");
                 }
                 elseif ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] != 1)
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' AND writer='" . $SF->SafeSQL($member_row['username']) . "' ORDER BY visitor DESC LIMIT $start," . $info_row['perpage'] ."");
                 }
             }

             if ($_GET['order_type'] == 'ASC')
             {
                 if (($section_row['hide_subject'] != 1) or ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] == 1))
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' ORDER BY visitor ASC LIMIT $start," . $info_row['perpage'] ."");
                 }
                 elseif ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] != 1)
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' AND writer='" . $SF->SafeSQL($member_row['username']) . "' ORDER BY visitor ASC LIMIT $start," . $info_row['perpage'] ."");
                 }
             }
         }
         elseif ($_GET['order'] == 3)
         {
             if ($_GET['order_type'] != 'DESC' and $_GET['order_type'] != 'ASC')
             {
                 $SF->error('الرابط الذي اتبعته غير صحيح');
             }

             if ($_GET['order_type'] == 'DESC')
             {
                 if (($section_row['hide_subject'] != 1) or ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] == 1))
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' ORDER BY reply_number DESC LIMIT $start," . $info_row['perpage'] ."");
                 }
                 elseif ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] != 1)
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' AND writer='" . $SF->SafeSQL($member_row['username']) . "' ORDER BY reply_number DESC LIMIT $start," . $info_row['perpage'] ."");
                 }
             }
             elseif ($_GET['order_type'] == 'ASC')
             {
                 if (($section_row['hide_subject'] != 1) or ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] == 1))
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' ORDER BY reply_number ASC LIMIT $start," . $info_row['perpage'] ."");
                 }
                 elseif ($section_row['hide_subject'] == 1 and $groupper_row['admincp_allow'] != 1)
                 {
                     $subject_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE delete_topic<>'1' AND stick<>'1' AND section='$id' AND writer='" . $SF->SafeSQL($member_row['username']) . "' ORDER BY reply_number ASC LIMIT $start," . $info_row['perpage'] ."");
                 }
             }
         }

         $subject_num = $DB->sql_num_rows($subject_query);

         $LastReply = array();
         while ($subjects_row = $DB->sql_fetch_array($subject_query))
         {
             $SF->html('<tr><td class="row3 tdb" width="3%" height="13">');

             eval($subjects_row['lastreply_cache']);

             $lastsharereply_num = in_array($member_row['username'],$LastReply);

             $reply_num = $subjects_row['reply_number'];

             $SF->subject_part4($subjects_row,$info_row,$member_row,$sharereply_num,$lastsharereply_num,$reply_num);
             $SF->subject_part4_2($subjects_row,$info_row,$member_row,$url_password);

			 
		if($reply_num == 0){
		$last_reply_number_page = 1;
		$start = 0;
		}else{
		$last_reply_number_page = ceil($reply_num/$info_row[subject_perpage]);
		$start = $info_row[subject_perpage]*($last_reply_number_page-1);
		

	         $lastreply_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "reply WHERE subject_id='" . $subjects_row['id'] . "' AND delete_topic<>'1' ORDER BY id DESC LIMIT 0,1");
            	 $lastreply_num   = $DB->sql_num_rows($lastreply_query);
	    	 $lastreply_row = $DB->sql_fetch_array($lastreply_query);


		}


		$Smarty->assign('last_reply_number_page',$last_reply_number_page);
		$Smarty->assign('start_no',$start);
		$Smarty->assign('reply_num',$reply_num);
		$Smarty->assign_by_ref('lastreply_row',$lastreply_row);


             $Smarty->display('section_subjects4.tpl');
         }

         echo "</table></div>";

         $url                  = "{$REQUEST_URI}";
         $url_array   = explode('ASC',$url);
         $url_array_e = explode('/',$url);
         $url_e                 = $url_array_e[1] . '/' . $url_array_e[2];

         if (count($url_array) == 2)
         {
             $url_e            = str_replace('ASC','DESC',$url);
             $type_print = 'تصاعدي';
         }
         elseif (count($url_array) == 1)
         {
             $url_e           = str_replace('DESC','ASC',$url);
             $type_print = 'تنازلي';
         }

         $url_e .= $SF->SafeOutPuts($url_password);
         $url_e .= '#posts_table';

         $Smarty->assign('s_id',$section_row['id']);
         $Smarty->assign('type_print',$type_print);
         $Smarty->assign('url',$url_e);

         $Smarty->display('section_bottom.tpl');

         $SF->FastMove();

         $Smarty->display('footer.tpl');
     }

?>